package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.CoopVisitBigShopInfoBean;
import cn.com.ghy.android.bean.CoopVisitBigShopInfoBeanParser;
import cn.com.ghy.android.bean.CoopVisitDealerInfoBean;
import cn.com.ghy.android.bean.CoopVisitDealerInfoBeanParser;
import cn.com.ghy.android.bean.CoopVisitInfoBean;
import cn.com.ghy.android.bean.CoopVisitInfoBeanParser;
import cn.com.ghy.android.bean.CoopVisitSmallShopInfoBean;
import cn.com.ghy.android.bean.CoopVisitSmallShopInfoBeanParser;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.XMLCreateException;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天协访服务器
 * @author wuz  @date 2011-6-20 10:49:24
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class VsPartOpeService {

	private String url = CommonStatic.URL_HEAD + "VsPartOpe_Ar.asmx";
	private String methodName = "";
	private Map<String, String> params = null;

	private static final String CUSTINFO = "custinfo";

	public List<CoopVisitInfoBean> GetVsDateInfo(String vstype, String vsid, String vsname,
			LoginInfoBean loginInfo, String vsdate) throws Exception {
		methodName = "GetVsDateInfo";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String xmlStr = CoopVisitInfoBeanParser.createXML(vstype, vsid, vsname, loginInfo, vsdate,
				writer);
		writer.close();
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CoopVisitInfoBean> list = CoopVisitInfoBeanParser.parseXML(in, vstype);
		in.close();
		return list;
	}

	public String SetVisitTime(String vstype, String vsid, String vstime, LoginInfoBean loginInfo,
			String vsdate) throws Exception {
		methodName = "SetVisitTime";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String xmlStr = CoopVisitInfoBeanParser.createVisitTimeXML(vstype, vsid, vstime, loginInfo,
				vsdate, writer);
		writer.close();
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public List<CoopVisitDealerInfoBean> GetVsPartDataForDealer(String vstype, String vsid,
			LoginInfoBean loginInfo, String vsdate) throws Exception {

		methodName = "GetVsPartData";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String xmlStr = CoopVisitInfoBeanParser.createVisitTimeXML(vstype, vsid, null, loginInfo,
				vsdate, writer);
		writer.close();
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = null;
		result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CoopVisitDealerInfoBean> list = CoopVisitDealerInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public List<CoopVisitBigShopInfoBean> GetVsPartDataForBigShop(String vstype, String vsid,
			LoginInfoBean loginInfo, String vsdate) throws Exception {

		methodName = "GetVsPartData";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String xmlStr = CoopVisitInfoBeanParser.createVisitTimeXML(vstype, vsid, null, loginInfo,
				vsdate, writer);
		writer.close();
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CoopVisitBigShopInfoBean> list = CoopVisitBigShopInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public List<CoopVisitSmallShopInfoBean> GetVsPartDataForSmallShop(String vstype, String vsid,
			LoginInfoBean loginInfo, String vsdate) throws Exception {

		methodName = "GetVsPartData";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String xmlStr = CoopVisitInfoBeanParser.createVisitTimeXML(vstype, vsid, null, loginInfo,
				vsdate, writer);
		writer.close();
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CoopVisitSmallShopInfoBean> list = CoopVisitSmallShopInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public String SendVsPartData(String vstype, Object o) throws Exception {
		methodName = "SendVsPartData";
		params = new HashMap<String, String>();
		StringWriter writer = new StringWriter();
		String xmlStr = null;
		if (o instanceof CoopVisitDealerInfoBean && vstype.equals(CommonStatic.VSTYPE_JXS)) {
			CoopVisitDealerInfoBean bean = (CoopVisitDealerInfoBean) o;
			xmlStr = CoopVisitDealerInfoBeanParser.createVsPartDataXMLForDealer(vstype, bean,
					writer);
		} else if (o instanceof CoopVisitBigShopInfoBean && vstype.equals(CommonStatic.VSTYPE_BIG)) {
			CoopVisitBigShopInfoBean bean = (CoopVisitBigShopInfoBean) o;
			xmlStr = CoopVisitBigShopInfoBeanParser.createVsPartDataXMLForBigShop(vstype, bean,
					writer);
		} else if (o instanceof CoopVisitSmallShopInfoBean
				&& vstype.equals(CommonStatic.VSTYPE_SMALL)) {
			CoopVisitSmallShopInfoBean bean = (CoopVisitSmallShopInfoBean) o;
			xmlStr = CoopVisitSmallShopInfoBeanParser.createVsPartDataXMLForSmallShop(vstype, bean,
					writer);
		}
		writer.close();
		if (null == xmlStr) {
			throw new XMLCreateException();
		}
		xmlStr = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(xmlStr.getBytes()));

		params.put(CUSTINFO, xmlStr);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}
}
